package hmis.pos.sys.shiro.controller;

import hmis.pos.Common.pojo.ApiResponse;
import hmis.pos.sys.entity.SysUserEntity;
import hmis.pos.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class WebController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/login")
    @ResponseBody
    public ApiResponse<SysUserEntity> login(@RequestBody SysUserEntity sysUserEntity){
        String username = sysUserEntity.getUsername();
        SysUserEntity entity = sysUserService.findByUsername(username);

        // 用户不存在或者用户密码不正确
        if(entity==null||!sysUserEntity.getPassword().equals(entity.getPassword())){
            return new ApiResponse<>(401,"用户名或密码错误");
        }
        // 登录成功
        if(sysUserEntity.getPassword().equals(entity.getPassword())) {
            return new ApiResponse<SysUserEntity>(200,"登录成功",entity);
        }
        // 以上条件都没有进入
        return new ApiResponse<>(401,"未知异常，请联系管理员");
    }

}
